package appapi.service.impl;

import appapi.Global.BusinessException;
import appapi.Request.EquiPageReq;
import appapi.Request.ProjPageReq;
import appapi.Request.ProjReq;
import appapi.dao.EquipmentMapper;
import appapi.dao.OperatorMapper;
import appapi.dao.OrganizationMapper;
import appapi.dao.ProjectMapper;
import appapi.dto.AddProjectDto;
import appapi.dto.UpdateProjectDto;
import appapi.entity.Zhongjiao.ExcelProjectDto;
import appapi.entity.Zhongjiao.OrganizationDto;
import appapi.entity.Zhongjiao.ProjectDto;
import appapi.service.IProjectService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author djq
 * @date 2023/5/11 16:35
 * @description:
 */
@Component
public class ProjectService implements IProjectService {

    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private OrganizationMapper organizationMapper;
    @Resource
    OrganizationImpl organization;
    @Resource
    OperatorMapper operatorMapper;
    @Resource
    EquipmentMapper equipmentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int InsertProject(AddProjectDto addDto) {
        if (projectMapper.selectByProjName(addDto.getProjName()) != null)
            throw new BusinessException(500, "该项目已存在!");

        OrganizationDto orgaDto = new OrganizationDto();
        orgaDto.setOrgaName(addDto.getProjName());
        orgaDto.setOrgaPid(addDto.getOrgaPid().intValue());
        //先增加组织机构
        Long orgaId = organization.addOrga(orgaDto);

        ProjectDto dto = new ProjectDto();
        BeanUtils.copyProperties(addDto, dto);
        dto.setOrgaId(orgaId);
        dto.setCreateTime(LocalDateTime.now());
        dto.setUpdateTime(LocalDateTime.now());
        int i = projectMapper.insertSelective(dto);

        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int UpdateProject(UpdateProjectDto updateDto) {
        if (projectMapper.selectByPrimaryKey(updateDto.getProjId()) == null)
            throw new BusinessException(500, "该项目不存在或已被删除!");

        ProjectDto dto = new ProjectDto();
        BeanUtils.copyProperties(updateDto, dto);
        dto.setUpdateTime(LocalDateTime.now());
        if (projectMapper.updateByPrimaryKeySelective(dto) == 0) {
            return 0;
        }
        OrganizationDto orgaDto = new OrganizationDto();
        orgaDto.setOrgaId(updateDto.getOrgaId());
        orgaDto.setOrgaName(updateDto.getProjName());
        orgaDto.setOrgaPid(updateDto.getOrgaPid().intValue());
        organization.updateOrga(orgaDto);

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int DelProject(Long projId) {

        ProjectDto dto = projectMapper.selectByPrimaryKey(projId);

        if (dto == null)
            throw new BusinessException(500, "该项目不存在或已被删除!");

        if (operatorMapper.selectAllByProjid(projId).size()>0){
            throw new BusinessException(500,"该项目下存在人员!不能删除!");
        }

        EquiPageReq req = new EquiPageReq();
        req.setProjId(projId);
        if (equipmentMapper.selectEquiPageList(req).size()>0){
            throw new BusinessException(500,"该项目下存在设备!不能删除!");
        }

        if (organizationMapper.deleteByPrimaryKey(dto.getOrgaId()) == 0) {
            return 0;
        }

        return projectMapper.deleteByPrimaryKey(projId);
    }

    @Override
    public List<Map<String, Object>> SelectProjectPage(ProjPageReq dto) {

        PageHelper.startPage(dto.getCurrPage(), dto.getPageSize());

        List<Map<String, Object>> projList = projectMapper.selectProjList(dto);

        return projList;
    }

    @Override
    public List<Map<String, Object>> getProjectList(ProjReq dto) {

        List<Map<String, Object>> projList = projectMapper.selectProjectList(dto);

        return projList;
    }


    @Override
    public Map<String, Object> SelectProjectDetail(Long projId) {
        Map<String, Object> projDetail = projectMapper.selectProjDetail(projId);
        List<Long> orgaPathIds = Arrays.stream(projDetail.get("orgaPathIds").toString().split("-")).map(map -> (Long.parseLong(map.trim()))).collect(Collectors.toList());
        projDetail.put("orgaArr", orgaPathIds);
        return projDetail;
    }

    @Override
    public List<ExcelProjectDto> getList(ProjPageReq req) {
        List<ExcelProjectDto> list = projectMapper.ProjectRecExport(req);
        return list;
    }


}
